
var FF3SudokuDissector = {
notify: function(doc, grid) {
	chrome.extension.connect().postMessage(grid);
},
grab_type2: function(doc, uri, letter) {
	var r, c;
	var out = "";
	for (r = 0;r<9;r++) {
		for (c = 0;c<9;c++) {
			var id = doc.getElementById(letter + c + r);
			if (null==id) {
				return false;	// not this type.
			}
			if (""==id.value) {
				out+= "0";
			} else {
				var n = id.value.charCodeAt(0)-48;
				out+= n;
			}
		}
	}
	FF3SudokuDissector.notify(doc, out);
	return true;
},
grab_type3: function(doc, uri, letter) {
	var r, c;
	var out = "";
	for (r = 1;r<10;r++) {
		for (c = 1;c<10;c++) {
			var id = doc.getElementById(letter + r + c);
			if (null==id) {
				return false;	// not this type.
			}
			if (""==id.value) {
				out+= "_";
			} else {
				var n = id.value.charCodeAt(0)-48;
				out+= n;
			}
		}
	}
	FF3SudokuDissector.notify(doc, out);
	return true;
},
grab_type1: function(doc, uri) {
	var r, c, i = 0;
	var out = "";
	try {
		for (r = 0;r<9;r++) {
			for (c = 0;c<9;c++) {
				var id = doc.getElementById("T"+i);
				id = id.childNodes[0];
				if (1==id.nodeType && "B"==id.nodeName) {
					out+= id.innerHTML;
				} else {
					out+= '_';
				}
				i+= 1;
			}
		}
	} catch(e) {
		return false;
	}
	FF3SudokuDissector.notify(doc, out);
	return true;
},
grab_type4: function(doc, uri, letter) {
	var i = 1;
	var out = "";
	try {
		for (i = 1;i<82;i++) {
			var id = doc.getElementById(letter + i);
			if (null==id) {
				return false;	// not this type.
			}
			if (""==id.value) {
				out+= "_";
			} else {
				var n = id.value.charCodeAt(0)-48;
				out+= n;
			}
		}
	} catch(e) {
		return false;
	}
	FF3SudokuDissector.notify(doc, out);
	return true;
},
grab_type5: function(doc, uri, letter) {
	var out = "";
	var map = [ 0, 1, 2, 9, 10, 11, 18, 19, 20 ];
	try {
		for (var row = 0;row<55;row+= 27) {
			for (var col = 0;col<8;col+= 3) {
				for (var i = 0;i<9;i++) {
					var idx = row+col+map[i];
					var n = doc.getElementById(letter + idx).innerHTML;
					if (n.match(/^[1-9]$/)) {
						out+= n;
					} else {
						out+= '_';
					}
				}
			}
		}
		FF3SudokuDissector.notify(doc, out);
		return true;
	} catch(e) {
		return false;
	}
},
detect: function(doc) {
	var uri="";
	if (	null!=doc.getElementById("sudokutable") &&
		null!=doc.getElementById("T0") && null!=doc.getElementById("T80")) {
		console.log("D1: maybe type 1"); // sudoku.com.au
		if (FF3SudokuDissector.grab_type1(doc, uri)) {
			return ;
		}
	}
	if (	null!=doc.getElementById("f00") && null!=doc.getElementById("f88")) {
		console.log("D1: maybe type 2"); // websudoku.com
		if (FF3SudokuDissector.grab_type2(doc, uri, "f")) {
			return ;
		}
	}
	if (	null!=doc.getElementById("inpt00") && null!=doc.getElementById("inpt88")) {
		console.log("D1: maybe type 2"); // http://www.online-sudoku.cz/hrat/
		if (FF3SudokuDissector.grab_type2(doc, uri, "inpt")) {
			return ;
		}
	}
	if (	null!=doc.getElementById("f11") && null!=doc.getElementById("f99")) {
		console.log("D1: maybe type 3 - f");	// www.sudokupuzz.com
		if (FF3SudokuDissector.grab_type3(doc, uri, 'f')) {
			return ;
		}
	}
	if (	null!=doc.getElementById("p1") && null!=doc.getElementById("p81")) {
		console.log("D1: maybe type 4 - p");	// http://sudokuonline.cz/
		if (FF3SudokuDissector.grab_type4(doc, uri, 'p')) {
			return ;
		}
	}
	if (	null!=doc.getElementById("i0") && null!=doc.getElementById("i80") && 
		null!=doc.getElementById("TAB0") && null!=doc.getElementById("TAB8")
	) {
		console.log("D1: maybe type 5 - p");	// sudoku.hu.cz sudoku.mobilhry.cz==www.lidovky.cz/sudoku.asp==sudoku.cz
		if (FF3SudokuDissector.grab_type5(doc, uri, 'i')) {
			return ;
		}
	}
},
__sentinel: 0
};

var ret = FF3SudokuDissector.detect(document);

window.addEventListener("focus", function() {FF3SudokuDissector.detect(document) });

